const app = getApp()
const request = require('../../utils/request.js')

Page({
  data: {
    moneyLog: [],
    hasData: false
  },

  onShow: function () {
    wx.setNavigationBarTitle({
      title: '收益明细',
    });

    request.moneyLog().then(res => {

      if(res.code == 0 && res.type == 2){
        let data = this.reversedSort(res.details);
        console.log('money---', data);
        for (let i = 0; i < data.length; i++) {
          data[i].ts = this.stamp2time(data[i].ts);
          let result = this.type2str(data[i].type, data[i].money/10000);
          data[i].type = result.split(',')[0];
          data[i].money = result.split(',')[1];
        }

        this.setData({
          moneyLog: data,
          hasData: data.length>0 ? true:false
        })
      }
    })
  },

  // sort
  reversedSort: function (data) {
    return data.sort(function (a, b) {
      if (a.ts > b.ts) {
        return -1;
      } else {
        return 1;
      }
    });
  },
  
  // share
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log("判断是否分享成功，是，添加步数");
      return {
        title: '转发一',
      }
    }
  },

  // 时间戳转时间
  stamp2time: function (stamp) {
    //如果date为13位不需要乘1000
    let date = new Date(stamp * 1000);
    let Y = date.getFullYear() + '/';
    let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '/';
    let D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
    // let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    // let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    // let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
    return Y + M + D;
  },

  // 判断类型
  type2str: function (types, money) {
    switch (types) {
      case "1000":
        return "收入," + Math.abs(money.toFixed(3));
        break;
      case "1001":
        return "提现," + (-Math.abs(money.toFixed(3)));
        break;
      default:
        return "";
    }
  }

})
